package cn.edu.sgu.www.pms.controller;

import cn.edu.sgu.www.pms.consts.CustomConstants;
import cn.edu.sgu.www.pms.consts.CustomMessages;
import cn.edu.sgu.www.pms.consts.MimeTypes;
import cn.edu.sgu.www.pms.dto.MenuDTO;
import cn.edu.sgu.www.pms.entity.Menu;
import cn.edu.sgu.www.pms.pager.impl.MenuPager;
import cn.edu.sgu.www.pms.restful.JsonPage;
import cn.edu.sgu.www.pms.restful.JsonResult;
import cn.edu.sgu.www.pms.service.MenuService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "菜单管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/menu", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class MenuController {

    private final MenuService menuService;

    @Autowired
    public MenuController(MenuService applicationService) {
        this.menuService = applicationService;
    }

    @ApiOperation("添加菜单")
    @ApiOperationSupport(ignoreParameters = "id")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated MenuDTO menuDTO) {
        menuService.insert(menuDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("通过ID批量删除菜单")
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    public JsonResult<Void> deleteByIds(
            @ApiParam(name = "ids", value = "菜单ID列表", required = true)
            @Size(min = 1, message = "菜单ID列表不能为空")
            @RequestParam("ids") List<String> ids) {
        menuService.deleteByIds(ids);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("通过ID修改菜单信息")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public JsonResult<Void> updateById(@Validated MenuDTO menuDTO) {
        menuService.updateById(menuDTO);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("查询全部父级菜单")
    @RequestMapping(value = "/selectParent", method = RequestMethod.GET)
    public JsonResult<List<Menu>> selectParent() {
        List<Menu> list = menuService.selectParent();

        return JsonResult.success(null, list);
    }

    @ApiOperation("通过ID查询菜单信息")
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    public JsonResult<Menu> selectById(
            @ApiParam(name = "id", value = "菜单ID", required = true)
            @NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
        Menu permission = menuService.selectById(id);

        return JsonResult.success(null, permission);
    }

    @ApiOperation("分页查询菜单列表")
    @RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
    public JsonResult<JsonPage<Menu>> selectByPage(MenuPager pager) {
        Page<Menu> selectByPage = menuService.selectByPage(pager);

        return JsonResult.restPage(selectByPage);
    }

}